%%%%%%%%%%%%%%
%%%%%%%%%%%%%%
% Data moments
%%%%%%%%%%%%%%
%%%%%%%%%%%%%%

[M_full,var_M_full,W_full,X_monthly,random_state]    ...
                                        = data_moments(root_folder,'');

%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%
% Initialize (pars,Nu)
%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%

[pars,Nu]                               = setup_function([],random_state,'');

%%%%%%%%%%%%
%%%%%%%%%%%%
% Estimate
%%%%%%%%%%%%
%%%%%%%%%%%%

k_target                                = 0.150;
xi_grid                                 = 0.0106;
verbose                                 = 0;
[output,SortResults,OutTable]           = estimate_k(k_target,pars,Nu,M_full,W_full,random_state,verbose,xi_grid);
results_baseline                        = output{1};
save([root_folder '/runs/' 'baseline_IRF.mat'],'results_baseline','pars','Nu');

%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%
% Check output
%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%

setup_inputs.xi                         = output{1}.XiFinalAllPars(1);
setup_inputs.sigma                      = output{1}.XiFinalAllPars(2);
setup_inputs.S                          = output{1}.XiFinalAllPars(4);
setup_inputs.M_e                        = output{1}.XiFinalAllPars(5); 
setup_inputs.C                          = output{1}.XiFinalAllPars(6);
[pars,Nu]                               = setup_function(setup_inputs,random_state,'');
Nu.targeted_pars                        = {'S','C','k','sigma','M_e'};
Nu.Npars                                = numel(Nu.targeted_pars);
Nu.fixed_pars                           = setdiff(Nu.all_pars,Nu.targeted_pars);
diagnostics(pars,M_full,W_full,Nu,0);

%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%
% Figure 10
%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%

% Output level
verbose                                 = 0;

% Save setup inputs
setup_inputs_saved                      = setup_inputs;

% Baseline
baseline                                = output{1};
pars_baseline                           = pars;
Nu_baseline                             = Nu;

% Counterfactual 0: no complementarities 
setup_inputs                            = setup_inputs_saved;
setup_inputs.C                          = 0.95*setup_inputs_saved.C;
[pars,Nu]                               = setup_function(setup_inputs,random_state,'');
cf0                                     = evaluate(pars,M_full,W_full,Nu,verbose);


% Counterfactual 1: faster mean-reversion 
setup_inputs                            = setup_inputs_saved;
setup_inputs.mean_reversion             = 60;
setup_inputs.shock_date                 = 15;
setup_inputs.C                          = 0.95*setup_inputs_saved.C;
setup_inputs.S                          = 0.9*setup_inputs.S;
[pars,Nu]                               = setup_function(setup_inputs,random_state,'');
cf1                                     = evaluate(pars,M_full,W_full,Nu,verbose);

% Counterfactual 2: smaller shock size   
setup_inputs                            = setup_inputs_saved;
setup_inputs.S                          = 0.8*setup_inputs.S;
[pars,Nu]                               = setup_function(setup_inputs,random_state,'');
cf2                                     = evaluate(pars,M_full,W_full,Nu,verbose);

% Plot
plot_all                                = 0;

if plot_all == 1

IRF_data                                = 100*M_full;
IRF_baseline                            = 100*mean(baseline.OutputFinal.M,2);
IRF_cf0                                 = 100*mean(cf0.OutputFinal.M,2);
IRF_cf0(7:end)                          = (IRF_baseline(end))/IRF_cf0(end)*IRF_cf0(7:end);
IRF_cf1                                 = 100*mean(cf1.OutputFinal.M,2);
IRF_cf2                                 = 100*mean(cf2.OutputFinal.M,2);

fig_10                                  = figure('Name','Figure 8','NumberTitle','off');
plot(1:numel(IRF_data),IRF_data,'-k','LineWidth',2); hold on;
plot(1:numel(IRF_data),IRF_baseline,'--b','LineWidth',2); hold on;
plot(1:numel(IRF_data),IRF_cf0,'-xr','LineWidth',2); hold on;
plot(1:numel(IRF_data),IRF_cf1,'-dr','LineWidth',2); hold on;
plot(1:numel(IRF_data),IRF_cf2,':r','LineWidth',2); hold on;
ylabel('\%','interpreter','latex','rotation',0);
xlabel('(month-end)','interpreter','latex','rotation',0);
ax                                      = gca;
ax.XAxis.Limits                         = [1 numel(IRF_data)];
ax.XAxis.TickValues                     = 1:numel(IRF_data);
ax.XAxis.TickLabels                     = {'May','Jun.','Jul.','Aug.','Sep.','Oct.','Nov.','Dec.','Jan.','Feb.','Mar.','Apr.','May','Jun.'};
ll                                      =legend('Data','Baseline','Counterfactual: no complementarities','Counterfactual: rapid currency swap','Counterfactual: smaller shock');
set(ll,'location','Northwest','interpreter','Latex');
tt                                      = title('Average change in adoption relative to October 2016 ($E_{t_0}\left[\Delta_{t_0}X_{d,t}\right]$)','interpreter','latex');
set(gca,'fontsize',12)
set(fig_10, 'Units', 'Normalized', 'OuterPosition', [0, 0.04, 0.5, 0.6]);    
set(fig_10,'Units','inches');
screenposition                          = get(fig_10,'Position');
set(fig_10,'PaperPosition',[0 0 screenposition(3:4)],'PaperSize',[screenposition(3:4)]);
fig_10.PaperPositionMode                ='auto';

else

IRF_data                                = 100*M_full;
IRF_baseline                            = 100*mean(baseline.OutputFinal.M,2);
IRF_cf0                                 = 100*mean(cf0.OutputFinal.M,2);
IRF_cf0(7:end)                          = (IRF_baseline(end))/IRF_cf0(end)*IRF_cf0(7:end);
IRF_cf1                                 = 100*mean(cf1.OutputFinal.M,2);
IRF_cf2                                 = 100*mean(cf2.OutputFinal.M,2);
IRF_data                                = IRF_data(6:end);
IRF_baseline                            = IRF_baseline(6:end);
IRF_cf0                                 = IRF_cf0(6:end);
IRF_cf1                                 = IRF_cf1(6:end);
IRF_cf2                                 = IRF_cf2(6:end);

fig_10                                  = figure('Name','Figure 8','NumberTitle','off');
plot(1:numel(IRF_data),IRF_data,'-k','LineWidth',2); hold on;
plot(1:numel(IRF_data),IRF_baseline,'--b','LineWidth',2); hold on;
plot(1:numel(IRF_data),IRF_cf0,'-xr','LineWidth',2); hold on;
plot(1:numel(IRF_data),IRF_cf1,'-dr','LineWidth',2); hold on;
plot(1:numel(IRF_data),IRF_cf2,':r','LineWidth',2); hold on;
ylabel('\%','interpreter','latex','rotation',0);
xlabel('(month-end)','interpreter','latex','rotation',0);
ax                                      = gca;
ax.XAxis.Limits                         = [1 numel(IRF_data)];
ax.XAxis.TickValues                     = 1:numel(IRF_data);
ax.XAxis.TickLabels                     = {'Oct.','Nov.','Dec.','Jan.','Feb.','Mar.','Apr.','May','Jun.'};
ll                                      =legend('Data','Baseline','Counterfactual: no complementarities','Counterfactual: rapid currency swap','Counterfactual: smaller shock');
set(ll,'location','Northwest','interpreter','Latex');
tt                                      = title('Average change in adoption relative to October 2016 ($E_{t_0}\left[\Delta_{t_0}X_{d,t}\right]$)','interpreter','latex');
set(gca,'fontsize',12)
set(fig_10, 'Units', 'Normalized', 'OuterPosition', [0, 0.04, 0.5, 0.6]);    
set(fig_10,'Units','inches');
screenposition                          = get(fig_10,'Position');
set(fig_10,'PaperPosition',[0 0 screenposition(3:4)],'PaperSize',[screenposition(3:4)]);
fig_10.PaperPositionMode                ='auto';

print(fig_10,[root_folder                        '/output/figures/Figure_8.pdf'],'-dpdf','-fillpage') 

end